﻿namespace Blaze.Editor
{
    using UnityEditor;
    using UnityEngine;

    /// <summary>
    /// <see cref="Transform"/>辅助工具类。
    /// </summary>
    public static class TransformTool
    {
        /// <summary>
        /// 显示<see cref="Transform"/>的完整路径。
        /// </summary>
        [MenuItem("CONTEXT/Transform/Copy Full Path")]
        public static void CopyTransformPath(MenuCommand cmd)
        {
            var transform = (Transform) cmd.context;
            EditorGUIUtility.systemCopyBuffer = getTransformPath(transform);
        }

        /// <summary>
        /// 显示<see cref="Transform"/>的矩阵信息。
        /// </summary>
        [MenuItem("CONTEXT/Transform/Copy Matrix")]
        public static void CopyTransformInfo(MenuCommand cmd)
        {
            var transform = (Transform)cmd.context;
            var output = string.Format("{0}{1}{2}", transform.localPosition.ToString("f2"), transform.localEulerAngles.ToString("f2"), transform.localScale.ToString("f2"));
            EditorGUIUtility.systemCopyBuffer = output;
        }

        private static string getTransformPath(Transform transform)
        {
            if (transform == null)
                return null;
            var basePath = getTransformPath(transform.parent);
            return basePath == null ? transform.name : string.Format("{0}/{1}", basePath, transform.name);
        }
    }
}